package org.jeecg.modules.purchase.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.purchase.entity.PurchaseOrder;
import org.jeecg.modules.purchase.entity.PurchaseOrderItem;
import org.jeecg.modules.purchase.mapper.PurchaseOrderItemMapper;
import org.jeecg.modules.purchase.mapper.PurchaseOrderMapper;
import org.jeecg.modules.purchase.service.IPurchaseOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 订单申请
 * @Author: jeecg-boot
 * @Date:   2022-11-14
 * @Version: V1.0
 */
@Service
public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, PurchaseOrder> implements IPurchaseOrderService {

	@Autowired
	private PurchaseOrderMapper purchaseOrderMapper;
	@Autowired
	private PurchaseOrderItemMapper purchaseOrderItemMapper;

	@Autowired
	private ISysBaseAPI sysBaseAPI;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(PurchaseOrder purchaseOrder, List<PurchaseOrderItem> purchaseOrderItemList) {
		if(StringUtils.isBlank(purchaseOrder.getCode())){
			String code = null;
			Result result = sysBaseAPI.executeByRuleCode("GoodsCodeRule",new JSONObject());
			if(result.isSuccess()){
				code = (String)result.getResult();
			}
			purchaseOrder.setCode(code);
		}
		purchaseOrderMapper.insert(purchaseOrder);
		if(purchaseOrderItemList!=null && purchaseOrderItemList.size()>0) {
			for(PurchaseOrderItem entity:purchaseOrderItemList) {
				//外键设置
				entity.setOrderId(purchaseOrder.getId());
				entity.setOrderCode(purchaseOrder.getCode());
				purchaseOrderItemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(PurchaseOrder purchaseOrder,List<PurchaseOrderItem> purchaseOrderItemList) {
		purchaseOrderMapper.updateById(purchaseOrder);
		
		//1.先删除子表数据
		purchaseOrderItemMapper.deleteByMainId(purchaseOrder.getId());
//		Date date = new Date();
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		String format = sdf.format(date);
		//2.子表数据重新插入
		if(purchaseOrderItemList!=null && purchaseOrderItemList.size()>0) {
			for(PurchaseOrderItem entity:purchaseOrderItemList) {
				//外键设置
				entity.setOrderCode(purchaseOrder.getCode());
				entity.setOrderId(purchaseOrder.getId());
				purchaseOrderItemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		purchaseOrderItemMapper.deleteByMainId(id);
		purchaseOrderMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			purchaseOrderItemMapper.deleteByMainId(id.toString());
			purchaseOrderMapper.deleteById(id);
		}
	}
	
}
